import { createRouter, createWebHashHistory } from "vue-router";
import { userStorer } from "@/stores/user";
import type { RouteRecordRaw } from "vue-router";
import NProgress from "nprogress";
import LocalCache from "@/utils/storage";
import { Message } from "@arco-design/web-vue";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: "/login",
  },

  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/login/index.vue"),
  },
  {
    path: "/admin",
    name: "Admin",
    component: () => import("@/views/admin/index.vue"),
    children: [
      {
        path: "person",
        name: "Person",
        meta: {
          title: "档案管理",
        },
        children: [
          {
            path: "employee",
            name: "Employee",
            meta: {
              title: "部门管理",
            },
            component: () => import("@/views/admin/person/employee/index.vue"),
          },
          // {
          //   path: "position",
          //   name: "Position",
          //   meta: {
          //     title: "职位管理",
          //   },
          //   component: () => import("@/views/admin/person/position/index.vue"),
          // },
          {
            path: "hotel",
            name: "Hotel",
            meta: {
              title: "宿舍管理",
            },
            component: () => import("@/views/admin/person/hotel/index.vue"),
          },
        ],
      },
      {
        path: "rule",
        name: "Rule",
        meta: {
          title: "规则管理",
        },
        component: () => import("@/views/admin/rule/index.vue"),
      },
      {
        path: "details",
        name: "Details",
        meta: {
          title: "明细汇总",
        },
        children: [
          {
            path: "record",
            name: "Record",
            meta: {
              title: "原始记录",
            },
            component: () => import("@/views/admin/details/record/index.vue"),
          },
          {
            path: "personDetail",
            name: "PersonDetail",
            meta: {
              title: "个人扣分明细",
            },
            component: () => import("@/views/admin/details/person/index.vue"),
          },
          {
            path: "hotelDetail",
            name: "HotelDetail",
            meta: {
              title: "宿舍扣分明细",
            },
            component: () => import("@/views/admin/details/hotel/index.vue"),
          },
          {
            path: "orgDetail",
            name: "OrgDetail",
            meta: {
              title: "部门扣分明细",
            },
            component: () => import("@/views/admin/details/org/index.vue"),
          },
          {
            path: "personSumDetail",
            name: "PersonSumDetail",
            meta: {
              title: "个人扣分汇总",
            },
            component: () =>
              import("@/views/admin/details/personsum/index.vue"),
          },
          {
            path: "hotelSumDetail",
            name: "HotelSumDetail",
            meta: {
              title: "宿舍扣分汇总",
            },
            component: () => import("@/views/admin/details/hotelsum/index.vue"),
          },
          {
            path: "orgSumDetail",
            name: "OrgSumDetail",
            meta: {
              title: "部门扣分汇总",
            },
            component: () => import("@/views/admin/details/orgsum/index.vue"),
          },
        ],
      },
    ],
  },
  {
    path: "/h5",
    name: "H5",
    children: [
      {
        path: "hLogin",
        name: "HLogin",
        component: () => import("@/views/h5/login/index.vue"),
      },
      {
        path: "hRule",
        name: "HRule",
        component: () => import("@/views/h5/rule/index.vue"),
      },
    ],
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  // to将要访问的路径
  // from从哪个路径而来
  // next表示放行执行
  const token = LocalCache.get("token");
  if (to.path === "/login") return next();
  if (to.path === "/h5/hRule") return next();
  if (!token) {
    Message.error("请先登录");
    return router.push({ name: "Login", query: { redirect: to.path } });
  }

  NProgress.configure({ showSpinner: false });
  NProgress.start();
  next();
});
router.afterEach(() => {
  NProgress.done();
});

export default router;
